package info.nixbear.tgcms.admin.client.mvp;

import info.nixbear.tgcms.admin.client.activity.PageEditActivity;
import info.nixbear.tgcms.admin.client.activity.PagesActivity;
import info.nixbear.tgcms.admin.client.activity.RedirectEditActivity;
import info.nixbear.tgcms.admin.client.activity.RedirectsActivity;
import info.nixbear.tgcms.admin.client.activity.TemplateEditActivity;
import info.nixbear.tgcms.admin.client.activity.TemplatesActivity;
import info.nixbear.tgcms.admin.client.place.PagesPlace;
import info.nixbear.tgcms.admin.client.place.PagesPlace.PageEditPlace;
import info.nixbear.tgcms.admin.client.place.RedirectsPlace;
import info.nixbear.tgcms.admin.client.place.RedirectsPlace.RedirectEditPlace;
import info.nixbear.tgcms.admin.client.place.TemplatesPlace;
import info.nixbear.tgcms.admin.client.place.TemplatesPlace.TemplateEditPlace;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.inject.Inject;
import com.google.inject.Provider;

public class AppActivityMapper implements ActivityMapper {

	Provider<PagesActivity> pagesActivityProvider;
	Provider<PageEditActivity> pageEditActivityProvider;
	Provider<TemplatesActivity> templatesActivityProvider;
	Provider<TemplateEditActivity> templateEditActivityProvider;
	Provider<RedirectsActivity> redirectsActivityProvider;
	Provider<RedirectEditActivity> redirectEditActivityProvider;

	/**
	 * AppActivityMapper associates each Place with its corresponding
	 * {@link Activity}
	 * 
	 * @param clientFactory
	 *            Factory to be passed to activities
	 */
	@Inject
	public AppActivityMapper(
			final Provider<PagesActivity> pagesActivityProvider,
			final Provider<PageEditActivity> pageEditActivityProvider,
			final Provider<TemplatesActivity> templatesActivityProvider,
			final Provider<TemplateEditActivity> templateEditActivityProvider,
			final Provider<RedirectsActivity> redirectsActivityProvider,
			final Provider<RedirectEditActivity> redirectEditActivityProvider
			) {
		super();
		this.pagesActivityProvider = pagesActivityProvider;
		this.pageEditActivityProvider = pageEditActivityProvider;
		this.templatesActivityProvider = templatesActivityProvider;
		this.templateEditActivityProvider = templateEditActivityProvider;
		this.redirectsActivityProvider = redirectsActivityProvider;
		this.redirectEditActivityProvider = redirectEditActivityProvider;
	}

	/**
	 * Map each Place to its corresponding Activity. This would be a great use
	 * for GIN.
	 */
	@Override
	public Activity getActivity(Place place) {
		// This is begging for GIN
		if (place instanceof PageEditPlace) {
			return pageEditActivityProvider.get().withPlace((PageEditPlace)place);
		} else if (place instanceof PagesPlace) {
			return pagesActivityProvider.get().withPlace((PagesPlace)place);
		} else if (place instanceof TemplateEditPlace) {
			return templateEditActivityProvider.get().withPlace((TemplateEditPlace) place);
		} else if (place instanceof TemplatesPlace) {
			return templatesActivityProvider.get().withPlace((TemplatesPlace)place);
		} else if (place instanceof RedirectEditPlace) {
			return redirectEditActivityProvider.get().withPlace((RedirectEditPlace) place);
		} else if (place instanceof RedirectsPlace) {
			return redirectsActivityProvider.get().withPlace((RedirectsPlace)place);
		}

		return null;
	}

}
